package com.system.config.mybatis.plugins;

import com.alibaba.fastjson.JSON;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.Arrays;
import java.util.Properties;

/**
 *  执行顺序 1 ： 查询操作执行前
 */
@Intercepts({
        @Signature(
                method = "query" ,
                type = Executor.class,args = {MappedStatement.class,Object.class, RowBounds.class, ResultHandler.class
})})
public class ExecutorPlugin implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("A:  " +invocation.getTarget() +" | "+  invocation.getMethod().getName());
        if(invocation.getTarget() instanceof  Executor){
            Object[] args = invocation.getArgs();
            // 拿到 参数后 可以 做 Redis 获取动作
            System.out.println("B:  " +invocation.getTarget() +" | "+  invocation.getMethod().getName() +" | " + Arrays.asList(args));

        }
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        if (target instanceof  Executor){
           return Plugin.wrap(target,this);
        }
        return target;
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
